//
//  MapCarLocate.m
//  CarCare
//
//  Created by ileo on 14/12/29.
//  Copyright (c) 2014年 baozun. All rights reserved.
//

#import "MapCarSearch.h"
#import "MANaviRoute.h"

@interface MapCarSearch()<MAMapViewDelegate, AMapSearchDelegate>

@property (nonatomic, strong) AMapSearchAPI *search;

@property (nonatomic, strong) MANaviRoute * naviRoute;
@property (nonatomic, strong) AMapRoute *route;

@property (nonatomic , assign) AMapRoutePlanningType routePlanningType;

@end

@implementation MapCarSearch

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.search = [[AMapSearchAPI alloc] init];
        self.search.delegate = self;
        [self.mapView setShowsUserLocation:YES];
        [self showUserLoc];
        self.mapView.delegate = self;
    }
    return self;
}

-(void)showCarLoc{
    [self.mapView setCenterCoordinate:self.coordinate animated:YES];
}

-(void)showUserLoc{
    [self.mapView setUserTrackingMode:MAUserTrackingModeFollow animated:YES];
}

-(void)wayToLoc:(AMapRoutePlanningType)searchLineType{
 
    self.routePlanningType = searchLineType;
    switch (searchLineType)
    {
        case AMapRoutePlanningTypeDrive:
        {
            [self searchRoutePlanningDrive];
            
            break;
        }
        case AMapRoutePlanningTypeWalk:
        {
            [self searchRoutePlanningWalk];
            
            break;
        }
        case AMapRoutePlanningTypeBus:
        {
            [self searchRoutePlanningBus];
            
            break;
        }
    }
}

#pragma mark - 

-(void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error{
    [AlertMgr showShortTextTips:@[@"无法获取路线"]];
    [self.naviRoute removeFromMapView];
}
-(void)onRouteSearchDone:(AMapRouteSearchBaseRequest *)request response:(AMapRouteSearchResponse *)response{
    if (response.route == nil)
    {
        return;
    }
    
    self.route = response.route;
    [self.naviRoute removeFromMapView];
    [self presentCurrentCourse];
}

/* 展示当前路线方案. */
- (void)presentCurrentCourse
{
    /* 公交路径规划. */
    if (self.routePlanningType == AMapRoutePlanningTypeBus)
    {
        if (self.route.transits.count == 0) {
            [AlertMgr showShortTextTips:@[@"无法获取路线"]];
            return;
        }
        
        self.naviRoute = [MANaviRoute naviRouteForTransit:self.route.transits[0]];
    }
    /* 步行，驾车路径规划. */
    else
    {
        if (self.route.paths.count == 0) {
            [AlertMgr showShortTextTips:@[@"无法获取路线"]];
            return;
        }
        
        MANaviAnnotationType type = self.routePlanningType == AMapRoutePlanningTypeDrive ? MANaviAnnotationTypeDrive : MANaviAnnotationTypeWalking;
        self.naviRoute = [MANaviRoute naviRouteForPath:self.route.paths[0] withNaviType:type];
    }
    
    [self.naviRoute addToMapView:self.mapView];
    
    /* 缩放地图使其适应polylines的展示. */
    NSInteger gap = 20;
    [self.mapView setVisibleMapRect:[CommonUtility mapRectForOverlays:self.naviRoute.routePolylines]
                        edgePadding:UIEdgeInsetsMake(gap, gap, gap, gap)
                           animated:YES];
}
/* 公交路径规划搜索. */
- (void)searchRoutePlanningBus
{
    AMapTransitRouteSearchRequest *navi = [[AMapTransitRouteSearchRequest alloc] init];
    
    navi.requireExtension = YES;
    navi.city             = self.currentCity;
    
    CLLocationCoordinate2D loca = self.mapView.userLocation.location.coordinate;
    navi.origin = [AMapGeoPoint locationWithLatitude:loca.latitude longitude:loca.longitude];
    navi.destination = [AMapGeoPoint locationWithLatitude:self.coordinate.latitude longitude:self.coordinate.longitude];
    
    [self.search AMapTransitRouteSearch:navi];
}

/* 步行路径规划搜索. */
- (void)searchRoutePlanningWalk
{
    AMapWalkingRouteSearchRequest *navi = [[AMapWalkingRouteSearchRequest alloc] init];
    
    /* 提供备选方案*/
    navi.multipath = 1;
    
    CLLocationCoordinate2D loca = self.mapView.userLocation.location.coordinate;
    navi.origin = [AMapGeoPoint locationWithLatitude:loca.latitude longitude:loca.longitude];
    navi.destination = [AMapGeoPoint locationWithLatitude:self.coordinate.latitude longitude:self.coordinate.longitude];
 
    [self.search AMapWalkingRouteSearch:navi];
}

/* 驾车路径规划搜索. */
- (void)searchRoutePlanningDrive
{
    AMapDrivingRouteSearchRequest *navi = [[AMapDrivingRouteSearchRequest alloc] init];
    
    navi.requireExtension = YES;
    
    CLLocationCoordinate2D loca = self.mapView.userLocation.location.coordinate;
    navi.origin = [AMapGeoPoint locationWithLatitude:loca.latitude longitude:loca.longitude];
    navi.destination = [AMapGeoPoint locationWithLatitude:self.coordinate.latitude longitude:self.coordinate.longitude];
    
    [self.search AMapDrivingRouteSearch:navi];
}

#pragma mark -

-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{
    
    if ([annotation isKindOfClass:[LocAnnotation class]]) {
        MAAnnotationView *annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"carAnnotation"];
        annotationView.image = ImageNamed(@"car_loc");
        annotationView.centerOffset = CGPointMake(0, -23);
        annotationView.canShowCallout = NO;
        return annotationView;
    }else if ([annotation isKindOfClass:[MAUserLocation class]]){
        MAAnnotationView *annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"userAnnotation"];
        annotationView.image = ImageNamed(@"user_loc");
        annotationView.centerOffset = CGPointMake(0, -23);
        return annotationView;
    }
    
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *routePlanningCellIdentifier = @"RoutePlanningCellIdentifier";
        
        MAAnnotationView *poiAnnotationView = (MAAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:routePlanningCellIdentifier];
        if (poiAnnotationView == nil)
        {
            poiAnnotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation
                                                             reuseIdentifier:routePlanningCellIdentifier];
        }
        
        poiAnnotationView.canShowCallout = YES;
        
        if ([annotation isKindOfClass:[MANaviAnnotation class]])
        {
            switch (((MANaviAnnotation*)annotation).type)
            {
                case MANaviAnnotationTypeBus:
                    poiAnnotationView.image = [UIImage imageNamed:@"bus"];
                    break;
                    
                case MANaviAnnotationTypeDrive:
                    poiAnnotationView.image = [UIImage imageNamed:@"car"];
                    break;
                    
                case MANaviAnnotationTypeWalking:
                    poiAnnotationView.image = [UIImage imageNamed:@"man"];
                    break;
                    
                default:
                    break;
            }
        }
        
        return poiAnnotationView;
    }
    
    return [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"normal"];
}

-(MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id<MAOverlay>)overlay{
    
    if ([overlay isKindOfClass:[LineDashPolyline class]])
    {
        MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:((LineDashPolyline *)overlay).polyline];
        
        polylineRenderer.lineWidth   = 5;
        polylineRenderer.strokeColor = COLOR_HEXSTRING(@"#1fb8d5");
        polylineRenderer.lineDashPattern = @[@5, @10];
        
        return polylineRenderer;
    }
    if ([overlay isKindOfClass:[MANaviPolyline class]])
    {
        MANaviPolyline *naviPolyline = (MANaviPolyline *)overlay;
        MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:naviPolyline.polyline];
        
        polylineRenderer.lineWidth   = 5;
        polylineRenderer.strokeColor = COLOR_HEXSTRING(@"#1fb8d5");
        
        return polylineRenderer;
    }
    
    return nil;
}


@end